<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/3/28
 * Time: 上午8:55
 * 余额变动操作
 */

namespace app\common\controller;
use app\common\model\CardMember;
use app\common\model\CardRank;
use think\Controller;
use Wechat\Wxobjopen;

class Level extends Wxoption
{



    //检查积分  所属等级
    /*
     * card_id
     * user:code bonus
     * */
    public function checklevel($card_id,$code,$bonus){
        $CardMember = new CardMember();
        $CardRank  = new CardRank();
        //获取对应等级
        $rank = $CardRank->where(['card_id'=>$card_id])->find();
        if ($rank) {
            $maps['card_id'] = array('eq', $card_id);
            $data['level'] = getrankname($card_id, $bonus);

            $data['code'] = $code;
            $data['card_id'] = $card_id;
            $res = $this->updatelevel($data);
        }else{
            $res = '无等级设置';
        }
        return $res;
    }

    //积分更新微信操作
    public function updatelevel($arr)
    {
        try {
            //实例化微信
            $options = $this->options($arr['card_id']);
            $WxObj = new Wxobjopen($options);
            $data = [
                'code' => $arr['code'],
                'card_id' => $arr['card_id'],
                'custom_field_value3' => $arr['level'],
                'notify_optional' => [
                    'is_notify_bonus' => false,
                    'is_notify_balance' => false,
                    'is_notify_custom_field1' => false,
                ],
            ];
            $res = $WxObj->updateUserInfo($data);
        } catch (\Exception $e) {
            $res = ['errcode' => 9999, 'message' => $e->getMessage()];
        }
        return $res;
    }



}